package com.ruoyi.gzmx_sblx_fjd.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gzmx_sblx_fjd.domain.Gzmx_sblx_fjd;
import com.ruoyi.gzmx_sblx_fjd.service.IGzmx_sblx_fjdService;
import com.ruoyi.gzmx_scdw.service.IGzmx_scdwService;
import com.ruoyi.system.service.ISysDeptService;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备类型Controller
 * 
 * @author ruoyi
 * @date 2022-10-21
 */
@Controller
@RequestMapping("/gzmx_sblx_fjd/fjd")
public class Gzmx_sblx_fjdController extends BaseController
{
    private String prefix = "gzmx_sblx_fjd/fjd";

    @Autowired
    private IGzmx_sblx_fjdService gzmx_sblx_fjdService;
    
    @Autowired
    private ISysDeptService deptService;
    
    @Autowired
    private IGzmx_scdwService gzmx_scdwService;

    @RequiresPermissions("gzmx_sblx_fjd:fjd:view")
    @GetMapping()
    public String fjd()
    {
        return prefix + "/fjd";
    }

    /**
     * 查询设备类型列表
     */
    @RequiresPermissions("gzmx_sblx_fjd:fjd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Gzmx_sblx_fjd gzmx_sblx_fjd)
    {
        startPage();
        List<Gzmx_sblx_fjd> list = gzmx_sblx_fjdService.selectGzmx_sblx_fjdList(gzmx_sblx_fjd);
        return getDataTable(list);
    }
    
    /**
     * 查询设备类型列表
     */
    //@RequiresPermissions("gzmx_sblx_fjd:fjd:list")
    @PostMapping("/list_all")
    @ResponseBody
    public TableDataInfo list_all(Gzmx_sblx_fjd gzmx_sblx_fjd)
    {
        
        List<Gzmx_sblx_fjd> list = gzmx_sblx_fjdService.selectGzmx_sblx_fjdList(gzmx_sblx_fjd);
        return getDataTable(list);
    }

    /**
     * 导出设备类型列表
     */
    @RequiresPermissions("gzmx_sblx_fjd:fjd:export")
    @Log(title = "设备类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Gzmx_sblx_fjd gzmx_sblx_fjd)
    {
        List<Gzmx_sblx_fjd> list = gzmx_sblx_fjdService.selectGzmx_sblx_fjdList(gzmx_sblx_fjd);
        ExcelUtil<Gzmx_sblx_fjd> util = new ExcelUtil<Gzmx_sblx_fjd>(Gzmx_sblx_fjd.class);
        return util.exportExcel(list, "设备类型数据");
    }

    /**
     * 新增设备类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备类型
     */
    @RequiresPermissions("gzmx_sblx_fjd:fjd:add")
    @Log(title = "设备类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Gzmx_sblx_fjd gzmx_sblx_fjd)
    {
    	System.out.println(ToFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写

        //System.out.println(ToPinyin("汉字转换为拼音"));
    	gzmx_sblx_fjd.setLx_name_jc(ToFirstChar(gzmx_sblx_fjd.getLx_name()).toUpperCase());
    	Gzmx_sblx_fjd gzmx_sblx_fjd_new=new Gzmx_sblx_fjd();
    	gzmx_sblx_fjd_new.setSf_jd(gzmx_sblx_fjd.getSf_jd());
    	gzmx_sblx_fjd_new.setLx_name(gzmx_sblx_fjd.getLx_name());
    	gzmx_sblx_fjd_new.setSblx(gzmx_sblx_fjd.getSblx());
    	List<Gzmx_sblx_fjd> list=gzmx_sblx_fjdService.selectGzmx_sblx_fjdList(gzmx_sblx_fjd_new);
    	for(int i=0;i<list.size();i++) {
    		if(list.get(i).getLx_name().equals(gzmx_sblx_fjd.getLx_name())) {
    			return error("类型名称已存在，请核对");
    		}
    	}
    	
    	
    	SysUser currentUser = ShiroUtils.getSysUser();
    	gzmx_sblx_fjd.setCreateBy(currentUser.getUserName());
    	SysDept dept=deptService.selectDeptById(currentUser.getDeptId());
    	if(dept.getAncestors().split(",").length>4) {
    		gzmx_sblx_fjd.setBm_id(currentUser.getDept().getDeptId());
    		gzmx_sblx_fjd.setBm_name(currentUser.getDept().getDeptName());
    		gzmx_sblx_fjd.setDept_id(currentUser.getDept().getParentId());
    		SysDept dept_new=deptService.selectDeptById(currentUser.getDept().getParentId());
    		gzmx_sblx_fjd.setDept_name(dept_new.getDeptName());
    	}else {
    		gzmx_sblx_fjd.setBm_id(currentUser.getDept().getDeptId());
    		gzmx_sblx_fjd.setBm_name(currentUser.getDept().getDeptName());
    		gzmx_sblx_fjd.setDept_id(currentUser.getDept().getDeptId());
    		gzmx_sblx_fjd.setDept_name(currentUser.getDept().getDeptName());
    	}
    	return toAjax(gzmx_sblx_fjdService.insertGzmx_sblx_fjd(gzmx_sblx_fjd));
    }

    /**
     * 修改设备类型
     */
    @RequiresPermissions("gzmx_sblx_fjd:fjd:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Gzmx_sblx_fjd gzmx_sblx_fjd = gzmx_sblx_fjdService.selectGzmx_sblx_fjdById(id);
        mmap.put("gzmx_sblx_fjd", gzmx_sblx_fjd);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备类型
     */
    @RequiresPermissions("gzmx_sblx_fjd:fjd:edit")
    @Log(title = "设备类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Gzmx_sblx_fjd gzmx_sblx_fjd)
    {
    	/*
    	Gzmx_scdw gzmx_scdw=new Gzmx_scdw();
    	gzmx_scdw.setJb(2L);;
    	List<Gzmx_scdw> list1= gzmx_scdwService.selectGzmx_scdwList(gzmx_scdw);
    	for(int i=0;i<list1.size();i++) {
    		if(StringUtil.isNotEmpty(list1.get(i).getDwmc_jc())) {
    			list1.get(i).setDwmc_jp(ToFirstChar(list1.get(i).getDwmc_jc()).toUpperCase());
    			gzmx_scdwService.updateGzmx_scdw(list1.get(i));
    			 
    		}
    		
    	}
    	*/
    	
    	gzmx_sblx_fjd.setLx_name_jc(ToFirstChar(gzmx_sblx_fjd.getLx_name()).toUpperCase());
    	Gzmx_sblx_fjd gzmx_sblx_fjd_new=new Gzmx_sblx_fjd();
    	gzmx_sblx_fjd_new.setSf_jd(gzmx_sblx_fjd.getSf_jd());
    	gzmx_sblx_fjd_new.setLx_name(gzmx_sblx_fjd.getLx_name());
    	gzmx_sblx_fjd_new.setSblx(gzmx_sblx_fjd.getSblx());
    	List<Gzmx_sblx_fjd> list=gzmx_sblx_fjdService.selectGzmx_sblx_fjdList(gzmx_sblx_fjd_new);
    	for(int i=0;i<list.size();i++) {
    		if(list.get(i).getLx_name().equals(gzmx_sblx_fjd.getLx_name())&&list.get(i).getId()!=gzmx_sblx_fjd.getId()) {
    			return error("类型名称已存在，请核对");
    		}
    	}
    	
    	SysUser currentUser = ShiroUtils.getSysUser();
    	gzmx_sblx_fjd.setUpdateBy(currentUser.getUserName());
    	return toAjax(gzmx_sblx_fjdService.updateGzmx_sblx_fjd(gzmx_sblx_fjd));
    }

    /**
     * 删除设备类型
     */
    @RequiresPermissions("gzmx_sblx_fjd:fjd:remove")
    @Log(title = "设备类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gzmx_sblx_fjdService.deleteGzmx_sblx_fjdByIds(ids));
    }
    
    /**

    * 获取字符串拼音的第一个字母

    * @param chinese

    * @return

    */

    public static String ToFirstChar(String chinese){

    String pinyinStr = "";

    char[] newChar = chinese.toCharArray(); //转为单个字符

    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

    for (int i = 0; i < newChar.length; i++) {

    if (newChar[i] > 128) {

    try {

    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);

    } catch (BadHanyuPinyinOutputFormatCombination e) {

    e.printStackTrace();

    }

    }else{

    pinyinStr += newChar[i];

    }

    }

    return pinyinStr;

    }

    /**

    * 汉字转为拼音

    * @param chinese

    * @return

    */

    public static String ToPinyin(String chinese){

    String pinyinStr = "";

    char[] newChar = chinese.toCharArray();

    HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

    defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

    defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

    for (int i = 0; i < newChar.length; i++) {

    if (newChar[i] > 128) {

    try {

    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];

    } catch (BadHanyuPinyinOutputFormatCombination e) {

    e.printStackTrace();

    }

    }else{

    pinyinStr += newChar[i];

    }

    }

    return pinyinStr;

    }

   

    
 
}
